<template>
	<view>
		<view class="ul">
			<u-checkbox-group v-model="checkboxValue" placement="column" @change="checkboxChange">
				<view class="li dis aic jcsb" v-for="(item, index) in list" :key="index">
					<view class="">
						<u-checkbox :customStyle="{marginBottom: '8px'}" shape="circle" activeColor="red"
							style="margin: 0  !important;" :name="item.goodId">
						</u-checkbox>
					</view>
					<view class="img_box">
						<image :src="item.image" mode="aspectFit" style="width: 100%;height: 100%;"></image>
					</view>
					<view class="">
						<view class="name">{{item.name}}</view>
						<view class="dis aic jcsb">
							<view class="price">￥{{item.price}}</view>
							<u-number-box :disabledInput=true button-size="28">
								<view slot="minus" class="minus">
									<u-icon name="minus" size="12" @click="delCar(item)"></u-icon>
								</view>
								<text slot="input" style="width: 50px;text-align: center;"
									class="input">{{item.number}}</text>
								<view slot="plus" class="plus">

									<u-icon name="plus" color="#000" size="12" @click="addCar(item.goodId)"></u-icon>
								</view>
							</u-number-box>
						</view>
					</view>
				</view>
			</u-checkbox-group>
		</view>
		<view class="btns dis aic  jcsb">
			<view>
				<u-radio-group v-model="isAll" @change='selectAll'>
					<u-radio shape="circle" label="全选" activeColor="red" name="123456"></u-radio>
				</u-radio-group>
			</view>
			<view class="dis aic ">
				<view class="number">共{{getSum}}件 合计：</view>
				<view class="sumprice">￥{{totalPrice }}</view>
				<view class="save" @click="goOrder">去支付<text class="sumnum">（{{getSum}}件）</text></view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		schoolCartList,
		cartDelete,
		cartAdd
	} from "@/api.js"
	export default {
		data() {
			return {
				value: 0,
				isAll: '',
				checkboxValue: [],
				list: [],
			};
		},
		onShow() {
			this.init()
		},
		computed: {
			// 计算选中商品总价
			totalPrice() {
				let arr = []
				for (let i = 0; i < this.list.length; i++) {
					for (let j = 0; j < this.checkboxValue.length; j++) {
						if (this.list[i].goodId == this.checkboxValue[j]) {
							arr.push(this.list[i])
						}
					}
				}
				return arr
					.reduce((sum, item) => {
						const price = parseFloat(item.price) || 0;
						const quantity = parseInt(item.number) || 0;
						return sum + price * quantity;
					}, 0)
					.toFixed(2); // 保留两位小数（如：53.80）
			},
			getSum() {
				let arr = []
				for (let i = 0; i < this.list.length; i++) {
					for (let j = 0; j < this.checkboxValue.length; j++) {
						if (this.list[i].goodId == this.checkboxValue[j]) {
							arr.push(this.list[i])
						}
					}
				}
				return arr.length
			},
			// getAll() {
			// 	let str = ''
			// 	if (this.checkboxValue.length == this.list.length) {
			// 		str = '123456'
			// 	}
			// 	this.isAll = str
			// }
		},
		methods: {
			goOrder() {
				if (this.checkboxValue.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '请先选择商品'
					})
					return
				}
				let arr = []
				for (let i = 0; i < this.list.length; i++) {
					for (let j = 0; j < this.checkboxValue.length; j++) {
						if (this.list[i].goodId == this.checkboxValue[j]) {
							arr.push(this.list[i])
						}
					}
				}
				uni.navigateTo({
					url: '/pages/shop/tuanGouConfirmOrder?detail=' + JSON.stringify(arr)
				})
			},
			init() {
				this.getList()
			},
			selectAll(v) {
				console.log('v', v, this.checkboxValue.length, this.list.length);
				uni.showLoading({
					title: '加载中',
					mask: true,
				})
				if (this.checkboxValue.length == this.list.length) {
					this.checkboxValue = []
					this.isAll = ''
				} else {
					this.checkboxValue = []
					for (let i = 0; i < this.list.length; i++) {
						this.checkboxValue.push(this.list[i].goodId)
					}
					this.isAll = '123456'
				}
				uni.hideLoading();
			},
			async addCar(id) {

				let res = await cartAdd({
					goodId: id,
					number: 1
				})
				if (res.code == 200) {
					// uni.showToast({
					// 	icon: "none",
					// 	title: '添加成功'
					// })
					this.getList()
				}
			},
			async delCar(item) {
				console.log('item', item);
				let msg = ''
				if (item.number == 1) {
					msg = '删除成功'
				}
				let res = await cartDelete({
					goodId: item.goodId,
					number: 1
				})

				if (res.code == 200 && msg) {
					uni.showToast({
						icon: "none",
						title: msg
					})
				}
				this.getList()
			},
			async getList() {
				let res = await schoolCartList({
					pageSize: 99999,
					pageNo: 1
				})
				if (res.code == 200) {
					this.list = res.result.records
				}
			},
			checkboxChange(n) {
				this.$nextTick(() => {
					if (this.checkboxValue.length == this.list.length) {
						this.isAll = '123456'
					} else {
						this.isAll = ''
					}
				})
				// setTimeout(() => {
				// 	console.log('c', this.checkboxValue.length, this.list.length);
				// if (this.checkboxValue.length == this.list.length) {
				// 	this.isAll = '123456'
				// } else {
				// 	this.isAll = ''
				// }
				// }, 200)
			},
		},
	}
</script>

<style lang="scss" scoped>
	.ul {
		margin: 0 30rpx;
		padding-bottom: calc(120rpx + env(safe-area-inset-bottom));

		.li {
			width: 690rpx;
			margin-bottom: 50rpx;

			.img_box {
				width: 220rpx;
				height: 220rpx;
			}

			.name {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #000000;
				width: 350rpx;
				margin-bottom: 90rpx;
			}

			.price {
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 30rpx;
				color: #8A3939;
			}
		}
	}

	.btns {
		width: 750rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		height: 120rpx;
		background: #FFFFFF;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		padding-bottom: env(safe-area-inset-bottom);

		.number {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #777777;
		}

		.sumprice {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 30rpx;
			color: #8A3939;
		}

		.save {
			line-height: 72rpx;
			text-align: center;
			width: 206rpx;
			height: 72rpx;
			background: linear-gradient(94deg, #F0BF75 0%, #D09943 100%);
			border-radius: 36rpx 36rpx 36rpx 36rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
			margin-left: 20rpx;

			.sumnum {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 24rpx;
				color: #FFFFFF;
			}
		}
	}

	.minus {
		width: 50rpx;
		height: 50rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: rgb(235, 236, 238);
		color: rgb(50, 50, 51);
	}

	.plus {
		width: 50rpx;
		height: 50rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: rgb(235, 236, 238);
		color: rgb(50, 50, 51);
	}
</style>